﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Thallo.Configuration {
    public sealed class InstrumentationProvider {

        const string CATEGORY_NAME = "Thallo";
        const string NAME = "Thallo Performance Counter";
        static bool enable = false;
        private PerformanceCounter cacheHits;
        private PerformanceCounter cacheMisses;
        private PerformanceCounter cacheAdds;

        static InstrumentationProvider() {
            if( ThalloSection.Current.Monitor )
                try {
                    enable = true;
                    SetupCategory();
                } catch (Exception) {
                    enable = false;
                }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void SetupCategory() {
            if (!PerformanceCounterCategory.Exists(CATEGORY_NAME)) {

                CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

                CounterCreationData a = new CounterCreationData();
                a.CounterType = PerformanceCounterType.NumberOfItems32;
                a.CounterName = "Cache Hits";
                CCDC.Add(a);

                CounterCreationData b = new CounterCreationData();
                b.CounterType = PerformanceCounterType.NumberOfItems32;
                b.CounterName = "Cache Misses";
                CCDC.Add(b);

                CounterCreationData c = new CounterCreationData();
                c.CounterType = PerformanceCounterType.NumberOfItems32;
                c.CounterName = "Cache Adds";
                CCDC.Add(c);

                PerformanceCounterCategory.Create(CATEGORY_NAME, NAME, PerformanceCounterCategoryType.MultiInstance, CCDC);
                
            }
        }

        private void InitCounter() {
            //enable = ThalloSection.Current.Monitor;
            if (enable) {
                string instanceName = ThalloSection.Current.Name;
                
                cacheHits = new PerformanceCounter(CATEGORY_NAME, "Cache Hits", instanceName, false);

                cacheMisses = new PerformanceCounter(CATEGORY_NAME, "Cache Misses", instanceName, false);

                cacheAdds = new PerformanceCounter(CATEGORY_NAME, "Cache Adds", instanceName,false);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void RemoveCategory() {
            if (PerformanceCounterCategory.Exists(CATEGORY_NAME)) {
                PerformanceCounterCategory.Delete(CATEGORY_NAME);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public InstrumentationProvider() {
            InitCounter();
        }



        /// <summary>
        /// 关闭计数
        /// </summary>
        public void Hits() {
            if (enable)
                cacheHits.Increment();

        }

        /// <summary>
        /// Post Close Connection Event
        /// </summary>
        public void Misses() {
            if (enable)
                cacheMisses.Increment();

        }

        /// <summary>
        /// 失败计数
        /// </summary>
        public void Addes() {
            if (enable)
                cacheAdds.Increment();
        }
    }
}
